package com.zhentao.service.impl;

import com.alibaba.fastjson.JSON;
import com.zhentao.apis.schedule.ScheduleClientApi;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.schedule.dto.TaskDto;
import com.zhentao.model.common.wemedia.WmNews;
import com.zhentao.service.WmNewsAutoScanService;
import com.zhentao.service.WmNewsTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;



@Service
public class WmNewsTaskServiceImpl implements WmNewsTaskService {
    @Autowired
    private ScheduleClientApi scheduleClientApi;
    @Autowired
    private WmNewsAutoScanService wmNewsAutoScanService;
    @Override
    public void addNewsToTask(WmNews wmNews) {
        System.out.println("添加任务到延迟任务里面");
        TaskDto taskDto = new TaskDto();
        taskDto.setTaskType(1);
        taskDto.setPriority(1);
        taskDto.setExecuteTime(wmNews.getPublishTime().getTime());
        taskDto.setParameters(JSON.toJSONBytes(wmNews));
        scheduleClientApi.addTask(taskDto);
    }
    //每秒钟执行一次
    @Scheduled(fixedRate = 1000)
    @Override
    public void scanNewsByTask(){
        System.out.println("执行提交审核的任务:"+System.currentTimeMillis());
        //从redis的list里面提取数据，添加的时候添加的是1，1
        ResponseResult result = scheduleClientApi.poll(1,1);
//        System.out.println("获取到redis里面的数据"+result);
        System.out.println(result.getCode());
        System.out.println("data数据为："+result.getData());
        if(result.getCode() == 200 && result.getData() != null){
            String dataJson = JSON.toJSONString(result.getData());
            TaskDto taskDto = JSON.parseObject(dataJson, TaskDto.class);
            WmNews wmNews = JSON.parseObject(taskDto.getParameters(), WmNews.class);
            System.out.println("获取到对象"+wmNews);
            //执行自动审核
            try {
                wmNewsAutoScanService.autoScanWmNews(wmNews.getId());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
}
